package org.javaforever.cookieshop.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.utils.BooleanUtil;
import org.javaforever.cookieshop.utils.PDFUtil;
import org.javaforever.cookieshop.utils.POIExcelUtil;
import org.javaforever.cookieshop.utils.POIPPTUtil;
import org.javaforever.cookieshop.utils.StringUtil;
import org.javaforever.cookieshop.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@RequestMapping(value = "/goodsController")
@RestController
public class GoodsController {
	public static GoodsBean tempGoods = new GoodsBean();
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected GoodsService service;

	@RequestMapping(value = "/activateAllGoodses", method = RequestMethod.POST)
	public Map<String,Object> activateAllGoodses(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateAllGoodses(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/activateGoods", method = RequestMethod.POST)
	public Map<String,Object> activateGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addGoods", method = RequestMethod.POST)
	public Map<String,Object> addGoods(@RequestBody GoodsBean goods) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        if (tempGoods!=null && tempGoods.getCover()!=null) {
            goods.setCover(tempGoods.getCover());
        }
        if (tempGoods!=null && tempGoods.getImage1()!=null) {
            goods.setImage1(tempGoods.getImage1());
        }
        if (tempGoods!=null && tempGoods.getImage2()!=null) {
            goods.setImage2(tempGoods.getImage2());
        }
        tempGoods = new  GoodsBean();
        service.addGoods(goods);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addUploadGoodsCover", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addUploadGoodsCover(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setCover(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/addUploadGoodsImage1", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addUploadGoodsImage1(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setImage1(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/addUploadGoodsImage2", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addUploadGoodsImage2(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setImage2(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/checkAccessGoods", method = RequestMethod.GET)
	public Map<String,Object> checkAccessGoods() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneAllGoodses", method = RequestMethod.POST)
	public Map<String,Object> cloneAllGoodses(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneAllGoodses(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneGoods", method = RequestMethod.POST)
	public Map<String,Object> cloneGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteAllGoodses", method = RequestMethod.POST)
	public Map<String,Object> deleteAllGoodses(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteAllGoodses(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
	public Map<String,Object> deleteGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/exportGoodses")
	public void exportGoodses(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.xls");
            List<GoodsBean> list = service.listAllGoodses();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type != null) {
                    typeType0 = typeService.findTypeById(type);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportGoodsesPDF")
	public void exportGoodsesPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.pdf");
            List<GoodsBean> list = service.listAllGoodses();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type != null) {
                    typeType0 = typeService.findTypeById(type);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportGoodsesWord")
	public void exportGoodsesWord(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.doc");
            List<GoodsBean> list = service.listAllGoodses();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type != null) {
                    typeType0 = typeService.findTypeById(type);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportPPTGoodses")
	public void exportPPTGoodses(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.pptx");
            List<GoodsBean> list = service.listAllGoodses();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type != null) {
                    typeType0 = typeService.findTypeById(type);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterGoodsesExcel", method = RequestMethod.GET)
	public void filterGoodsesExcel(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "price", required = false) Double price,
		@RequestParam(value = "intro", required = false) String intro,
		@RequestParam(value = "stock", required = false) Integer stock,
		@RequestParam(value = "type", required = false) Long type,
		@RequestParam(value = "isScroll", required = false) String isScroll,
		@RequestParam(value = "isHot", required = false) String isHot,
		@RequestParam(value = "isNew", required = false) String isNew) throws Exception{
        GoodsBean goods0 = new GoodsBean();
        if (!StringUtil.isBlank(name)) goods0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) goods0.setActive(activeBool);
        if (price != null) goods0.setPrice(price);
        if (!StringUtil.isBlank(intro)) goods0.setIntro(intro);
        if (stock != null) goods0.setStock(stock);
        if (type != null) goods0.setType(type);
        Boolean isScrollBool = BooleanUtil.parseBoolean(isScroll);
        if (isScrollBool != null) goods0.setIsScroll(isScrollBool);
        Boolean isHotBool = BooleanUtil.parseBoolean(isHot);
        if (isHotBool != null) goods0.setIsHot(isHotBool);
        Boolean isNewBool = BooleanUtil.parseBoolean(isNew);
        if (isNewBool != null) goods0.setIsNew(isNewBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.xls");
            List<GoodsBean> list = service.searchGoodsesByFields(goods0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type0 = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type0 != null) {
                    typeType0 = typeService.findTypeById(type0);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterGoodsesPDF", method = RequestMethod.GET)
	public void filterGoodsesPDF(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "price", required = false) Double price,
		@RequestParam(value = "intro", required = false) String intro,
		@RequestParam(value = "stock", required = false) Integer stock,
		@RequestParam(value = "type", required = false) Long type,
		@RequestParam(value = "isScroll", required = false) String isScroll,
		@RequestParam(value = "isHot", required = false) String isHot,
		@RequestParam(value = "isNew", required = false) String isNew) throws Exception{
        GoodsBean goods0 = new GoodsBean();
        if (!StringUtil.isBlank(name)) goods0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) goods0.setActive(activeBool);
        if (price != null) goods0.setPrice(price);
        if (!StringUtil.isBlank(intro)) goods0.setIntro(intro);
        if (stock != null) goods0.setStock(stock);
        if (type != null) goods0.setType(type);
        Boolean isScrollBool = BooleanUtil.parseBoolean(isScroll);
        if (isScrollBool != null) goods0.setIsScroll(isScrollBool);
        Boolean isHotBool = BooleanUtil.parseBoolean(isHot);
        if (isHotBool != null) goods0.setIsHot(isHotBool);
        Boolean isNewBool = BooleanUtil.parseBoolean(isNew);
        if (isNewBool != null) goods0.setIsNew(isNewBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.pdf");
            List<GoodsBean> list = service.searchGoodsesByFields(goods0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type0 = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type0 != null) {
                    typeType0 = typeService.findTypeById(type0);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterGoodsesWord", method = RequestMethod.GET)
	public void filterGoodsesWord(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "price", required = false) Double price,
		@RequestParam(value = "intro", required = false) String intro,
		@RequestParam(value = "stock", required = false) Integer stock,
		@RequestParam(value = "type", required = false) Long type,
		@RequestParam(value = "isScroll", required = false) String isScroll,
		@RequestParam(value = "isHot", required = false) String isHot,
		@RequestParam(value = "isNew", required = false) String isNew) throws Exception{
        GoodsBean goods0 = new GoodsBean();
        if (!StringUtil.isBlank(name)) goods0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) goods0.setActive(activeBool);
        if (price != null) goods0.setPrice(price);
        if (!StringUtil.isBlank(intro)) goods0.setIntro(intro);
        if (stock != null) goods0.setStock(stock);
        if (type != null) goods0.setType(type);
        Boolean isScrollBool = BooleanUtil.parseBoolean(isScroll);
        if (isScrollBool != null) goods0.setIsScroll(isScrollBool);
        Boolean isHotBool = BooleanUtil.parseBoolean(isHot);
        if (isHotBool != null) goods0.setIsHot(isHotBool);
        Boolean isNewBool = BooleanUtil.parseBoolean(isNew);
        if (isNewBool != null) goods0.setIsNew(isNewBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.doc");
            List<GoodsBean> list = service.searchGoodsesByFields(goods0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type0 = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type0 != null) {
                    typeType0 = typeService.findTypeById(type0);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPPTGoodses", method = RequestMethod.GET)
	public void filterPPTGoodses(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "price", required = false) Double price,
		@RequestParam(value = "intro", required = false) String intro,
		@RequestParam(value = "stock", required = false) Integer stock,
		@RequestParam(value = "type", required = false) Long type,
		@RequestParam(value = "isScroll", required = false) String isScroll,
		@RequestParam(value = "isHot", required = false) String isHot,
		@RequestParam(value = "isNew", required = false) String isNew) throws Exception{
        GoodsBean goods0 = new GoodsBean();
        if (!StringUtil.isBlank(name)) goods0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) goods0.setActive(activeBool);
        if (price != null) goods0.setPrice(price);
        if (!StringUtil.isBlank(intro)) goods0.setIntro(intro);
        if (stock != null) goods0.setStock(stock);
        if (type != null) goods0.setType(type);
        Boolean isScrollBool = BooleanUtil.parseBoolean(isScroll);
        if (isScrollBool != null) goods0.setIsScroll(isScrollBool);
        Boolean isHotBool = BooleanUtil.parseBoolean(isHot);
        if (isHotBool != null) goods0.setIsHot(isHotBool);
        Boolean isNewBool = BooleanUtil.parseBoolean(isNew);
        if (isNewBool != null) goods0.setIsNew(isNewBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Goodses.pptx");
            List<GoodsBean> list = service.searchGoodsesByFields(goods0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "货品信息表";
            String [] headers = {"序号","名字","活跃","封面","图片一","图片二","价格","简介","存货","类型","滚动","热门商品","等级"};
            Boolean [] isImages = {false,false,false,true,true,true,false,false,false,false,false,false,false};
            
            for (GoodsBean goods:list) {
                Long type0 = goods.getType();
                TypeBean typeType0;
                String typeName0 = "";
                if (type0 != null) {
                    typeType0 = typeService.findTypeById(type0);
                    if (typeType0 !=null) typeName0 = typeType0.getName();
                }
                Object [] row = {StringUtil.toNullString(goods.getId()),goods.getName(),StringUtil.toNullString(goods.getActive()),goods.getCover(),goods.getImage1(),goods.getImage2(),StringUtil.toNullString(goods.getPrice()),goods.getIntro(),StringUtil.toNullString(goods.getStock()),typeName0,StringUtil.toNullString(goods.getIsScroll()),StringUtil.toNullString(goods.getIsHot()),StringUtil.toNullString(goods.getIsNew())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/findGoodsById", method = RequestMethod.POST)
	public Map<String,Object> findGoodsById(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        GoodsBean goods = service.findGoodsById(id);
        result.put("success",true);
        result.put("data",goods);
        return result;
	}
	@RequestMapping(value = "/findGoodsByName", method = RequestMethod.POST)
	public Map<String,Object> findGoodsByName(@RequestParam(value = "name", required = true) String name) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        GoodsBean goods = service.findGoodsByName(name);
        result.put("success",true);
        result.put("data",goods);
        return result;
	}
	@RequestMapping(value = "/listActiveGoodses", method = RequestMethod.POST)
	public Map<String,Object> listActiveGoodses() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<GoodsBean> list = service.listActiveGoodses();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAllGoodses", method = RequestMethod.POST)
	public Map<String,Object> listAllGoodses() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<GoodsBean> list = service.listAllGoodses();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/searchGoodsesByFieldsByPage", method = RequestMethod.POST)
	public Map<String,Object> searchGoodsesByFieldsByPage(@RequestParam(value = "page", required = false) Integer pagenum,
		@RequestParam(value = "rows", required = false) Integer pagesize,
		@RequestParam(value = "last", required = false) String lastFlag,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "price", required = false) Double price,
		@RequestParam(value = "intro", required = false) String intro,
		@RequestParam(value = "stock", required = false) Integer stock,
		@RequestParam(value = "type", required = false) Long type,
		@RequestParam(value = "isScroll", required = false) String isScroll,
		@RequestParam(value = "isHot", required = false) String isHot,
		@RequestParam(value = "isNew", required = false) String isNew) throws Exception{
        GoodsBean goods = new GoodsBean();
        if (!StringUtil.isBlank(name)) goods.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) goods.setActive(activeBool);
        if (price != null) goods.setPrice(price);
        if (!StringUtil.isBlank(intro)) goods.setIntro(intro);
        if (stock != null) goods.setStock(stock);
        if (type != null) goods.setType(type);
        Boolean isScrollBool = BooleanUtil.parseBoolean(isScroll);
        if (isScrollBool != null) goods.setIsScroll(isScrollBool);
        Boolean isHotBool = BooleanUtil.parseBoolean(isHot);
        if (isHotBool != null) goods.setIsHot(isHotBool);
        Boolean isNewBool = BooleanUtil.parseBoolean(isNew);
        if (isNewBool != null) goods.setIsNew(isNewBool);
        if (pagesize==null || pagesize <= 0) pagesize = 10;
        Integer recordCount = service.countSearchGoodsesByFieldsRecords(goods);
        Integer pageCount = (int)Math.ceil((double)recordCount/pagesize);
        if (pageCount <= 1) pageCount = 1;
        if (pagenum==null || pagenum <= 1) pagenum = 1;
        if (pagenum >= pageCount) pagenum = pageCount;
        Boolean lastFlagBool = BooleanUtil.parseBoolean(lastFlag);
        if (lastFlagBool == null) lastFlagBool = false;
        if (lastFlagBool) pagenum = pageCount;
        Map<String,Object> result = new TreeMap<String,Object>();
        List<GoodsBean> goodsList = service.searchGoodsesByFieldsByPage(goods,pagesize,pagenum);
        result.put("success",true);
        result.put("rows",goodsList);
        result.put("total",recordCount);
        result.put("page",pagenum);
        return result;
	}
	@RequestMapping(value = "/softDeleteAllGoodses", method = RequestMethod.POST)
	public Map<String,Object> softDeleteAllGoodses(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteAllGoodses(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/softDeleteGoods", method = RequestMethod.POST)
	public Map<String,Object> softDeleteGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleGoods", method = RequestMethod.POST)
	public Map<String,Object> toggleGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleOneGoods", method = RequestMethod.POST)
	public Map<String,Object> toggleOneGoods(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleOneGoods(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
	public Map<String,Object> updateGoods(@RequestBody GoodsBean goods) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        GoodsBean oriGoods = service.findGoodsById(goods.getId());
        if (oriGoods.getCover()!=null) {
            goods.setCover(oriGoods.getCover());
        }
        if (oriGoods.getImage1()!=null) {
            goods.setImage1(oriGoods.getImage1());
        }
        if (oriGoods.getImage2()!=null) {
            goods.setImage2(oriGoods.getImage2());
        }
        if (tempGoods!=null && tempGoods.getCover()!=null) {
            goods.setCover(tempGoods.getCover());
        }
        if (tempGoods!=null && tempGoods.getImage1()!=null) {
            goods.setImage1(tempGoods.getImage1());
        }
        if (tempGoods!=null && tempGoods.getImage2()!=null) {
            goods.setImage2(tempGoods.getImage2());
        }
        tempGoods = new  GoodsBean();
        service.updateGoods(goods);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updateUploadGoodsCover", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateUploadGoodsCover(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setCover(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/updateUploadGoodsImage1", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateUploadGoodsImage1(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setImage1(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/updateUploadGoodsImage2", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateUploadGoodsImage2(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempGoods == null) tempGoods = new GoodsBean();
                tempGoods.setImage2(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
}

